<template>
  <div class="goodInfo">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    <div class="mui-card">
      <div class="mui-card-content">
        <swiper :img="img" :isfull="false"></swiper>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-header">{{ goodInfoData.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="sell">
            市场价：
            <span class="old">￥{{ goodInfoData.market_price }}&nbsp;&nbsp;</span>
            销售价：
            <span class="new">￥{{ goodInfoData.sell_price }}&nbsp;&nbsp;</span>
          </p>
          <p class="count">
            购买数量：
            <numbox @getCount="getCount" :max="goodInfoData.stock_quantity"></numbox>
          </p>
          <div class="btn">
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addCart">加入购物车</mt-button>
          </div>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品编号：{{ goodInfoData.goods_no }}</p>
          <p>库存情况：{{ goodInfoData.stock_quantity }}</p>
          <p>上架时间：{{ goodInfoData.add_time }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goodsIntroduce(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goodsComments(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swiper from "../subcomponents/swiper.vue";
import numbox from "../subcomponents/numbox.vue";
export default {
  created() {
    this.getImg();
    this.goodInfo();
  },
  data() {
    return {
      img: [],
      id: this.$route.params.id,
      goodInfoData: {},
      ballFlag: false,
      changeCount: 1
    };
  },
  methods: {
    getImg() {
      this.$http.get("api/getthumimages/" + this.id).then(data => {
        data.body.message.forEach(item => {
          item.img = item.src;
        });
        this.img = data.body.message;
      });
    },
    goodInfo() {
      this.$http.get("api/goods/getinfo/" + this.id).then(data => {
        if (data.body.status === 0) {
          //   console.log(data.body.message[0]);
          this.goodInfoData = data.body.message[0];
        }
      });
    },
    goodsIntroduce(id) {
      this.$router.push({ name: "goodsIntroduce", params: id });
    },
    goodsComments(id) {
      this.$router.push("/home/goodsComments/" + this.id);
    },
    addCart() {
      this.ballFlag = !this.ballFlag;
      var goodsInfo = {
        id: this.id,
        selected: true,
        count: this.changeCount,
        price: this.goodInfoData.sell_price
      };
      console.log(this.goodInfoData)
      // 调用store中的mutations
      this.$store.commit("addCart", goodsInfo);
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      const ballPositon = this.$refs.ball.getBoundingClientRect();
      const badgePostion = document
        .getElementById("badge")
        .getBoundingClientRect();
      const x = badgePostion.left - ballPositon.left;
      const y = badgePostion.top - ballPositon.top;
      el.offsetWidth;
      el.style.transition = "all 1s cubic-bezier(.4,-0.3,1,.68)";
      el.style.transform = `translate(${x}px,${y}px)`;
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getCount(count) {
      // 子组件的count值传到父组件
      this.changeCount = count;
      // console.log(this.changeCount)
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>
<style lang="scss" scoped>
.goodInfo {
  overflow: hidden;
  background-color: #eee;
  .ball {
    width: 15px;
    height: 15px;
    background-color: red;
    border-radius: 50%;
    position: absolute;
    top: 345px;
    left: 140px;
    z-index: 999;
    // transform: translate(100px,345px);
  }
  p {
    font-size: 12px;
    margin: 0px;
  }
  .sell {
    .old {
      text-decoration: line-through;
    }
    .new {
      font-size: 14px;
      color: red;
    }
  }
  .count {
    margin-bottom: 5px;
  }
  .mui-card-footer {
    display: block;
    :nth-child(1) {
      margin-bottom: 15px;
    }
  }
  .btn {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
